﻿
//-------------------------------------------------------------------------------
// <copyright file="SwfObjectDesigner.cs" company="GrannetDotNet Software">
//     Copyright © 2009 GrannetDotNet Software. All rights reserved.
// </copyright>
// <author>Grant Harmeyer</author>
//-------------------------------------------------------------------------------

namespace GrannetDotNet.Web.UI.WebControls
{
    using System;
    using System.Drawing;
    using System.IO;
    using System.Web.UI;
    using System.Web.UI.Design;
    using System.Web.Configuration;

    /// <summary>
    /// Designer control for the SwfObject control
    /// </summary>
    internal class SwfObjectDesigner 
        : ControlDesigner
    {
        #region [ Fields ]
        #endregion

        #region [ Methods ]

        /// <summary>
        /// Gets the HTML for the designer when the control is empty
        /// </summary>
        /// <returns>Design time HTML</returns>
        protected override string GetEmptyDesignTimeHtml()
        {
            return string.Empty;
        }

        /// <summary>
        /// Obtains the HTML for the designer in the event of an control error
        /// </summary>
        /// <param name="e">Error encountered during the design process</param>
        /// <returns>Design time HTML</returns>
        protected override string GetErrorDesignTimeHtml(System.Exception e)
        {
            return e.Message;
        }

        /// <summary>
        /// Obtains the HTML for design time support
        /// </summary>
        /// <returns>Design time HTML</returns>
        public override string GetDesignTimeHtml()
        {
            HtmlTextWriter            writer     = null;
            string                    retVal     = null;
            StringWriter              sw         = null;
            SwfObject                 swfObj     = null;
            SwfObjectAlternateContent altContent = null;

            try
            {
                /* Capture the component in design */
                swfObj = base.Component as SwfObject;
                            
                /* Render Design time HTML */
                sw     = new StringWriter();
                writer = new HtmlTextWriter(sw);

                if (swfObj.BackColor != default(Color))
                {
                    writer.AddStyleAttribute(HtmlTextWriterStyle.BackgroundColor, ColorTranslator.ToHtml(swfObj.BackColor));
                }

                writer.AddStyleAttribute(HtmlTextWriterStyle.Height,    swfObj.Height.ToString());
                writer.AddStyleAttribute("line-height",                 swfObj.Height.ToString());
                writer.AddStyleAttribute(HtmlTextWriterStyle.TextAlign, "center");
                writer.AddStyleAttribute(HtmlTextWriterStyle.Width,     swfObj.Width.ToString());
                writer.RenderBeginTag(HtmlTextWriterTag.Div);

                if (swfObj.AlternateContentTemplate != null)
                {
                    altContent = new SwfObjectAlternateContent();
                    swfObj.AlternateContentTemplate.InstantiateIn(altContent);
                    altContent.RenderControl(writer);
                }
                else
                {
                    writer.Write("GdnSwfObject");
                }
                
                writer.RenderEndTag();

                retVal = sw.ToString();
            }
            catch (Exception e)
            {                
                retVal = this.GetErrorDesignTimeHtml(e);
            }

            return retVal;
        }

        #endregion
    }
}